var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
app.title = '北京公交路线 - 线特效';

function getData(point) {
    console.info("哈哈")
    var A = point[0],
        B = point[1],
        n = 30,
        arc = Math.PI / 4,
        points = [];

    function getCircleRadio(A, B, arc) {
        return Math.sqrt(Math.pow(B[0]-A[0], 2)+Math.pow(B[1]-A[1], 2)) / (2*Math.sin(arc/2));
    }

    function getCircleOrigin(A, B, r) {
        var x0 = 0, y0 = 0;
        if (A[0] == B[0]) {
            x0 = A[0] - Math.sqrt( r*r - Math.pow((B[1]-A[1]/2), 2));
            y0 = (A[1] + B[1])/2;
        } else if (A[1] == B[1]) {
            x0 = (A[0] + B[0])/2;
            y0 = A[1] + Math.sqrt( r*r - Math.pow((B[0]-A[0]/2), 2));
        } else {
            var M = [(A[0] + B[0])/2, (A[1] + B[1])/2],
                k = (A[0]-B[0])/(B[1]-A[1]),
                a = 1.0 + k * k,
                b = -2 * M[0] - k * k * (A[0] + B[0]),
                c = M[0] * M[0] + k * k * (A[0] + B[0]) * (A[0] + B[0]) / 4.0 - (r * r - ((M[0] - A[0]) * (M[0] - A[0]) + (M[1] - A[1]) * (M[1] - A[1])));
            x0 = (-1.0 * b - Math.sqrt(b * b -4 * a * c)) / (2 * a);
            y0 = k * x0 - k * M[0] + M[1];
        }
        return [x0, y0];
    }
    var R = getCircleRadio(A, B, arc);
    var Ori = getCircleOrigin(A, B, R)
    for (var i=0; i<n; i++) {
        var angl = arc*i / n,
            arc0 = Math.asin((A[0]-Ori[0])/R);
        var x = Ori[0] + Math.sin(arc0 + angl)*R;
        var y = Ori[1] + Math.cos(arc0 + angl)*R;
        points.push([x, y]);
    }
    return points;
}

function getworkData(busData){

    console.info("开始调用后台服务！");

    var lines = $.ajax({
        type: "get",
        url: "http://127.0.0.1:14028/webgis/playdayod/get/"+"05",
        dataType : 'json',
        async: false
    }).then(function (data) {
        var hStep = 300 / (data.length - 1);
        var busLines = [].concat.apply([], data.map(function (busLine, idx) {
            // console.log(busLine);
            // console.log("busLine对象" + busLine +"busLine长度："+ busLine.length);
            return {
                coords: busLine,
                lineStyle: {
                    normal: {
                        color: echarts.color.modifyHSL('#5A94DF', Math.round(hStep * idx))
                    }
                }
            };

        }));
        busData = busLines;
        console.log("busData数据："+busData)
        return busData;
    });
}

var busData = [];
$.get('js/lines-bus.json', function(data) {
    var lines = $.ajax({
        type: "get",
        url: "http://127.0.0.1:14028/webgis/playdayod/get/"+"01",
        dataType : 'json',
        async: false,
        success: function (data) {
            var hStep = 300 / (data.length - 1);
            var busLines = [].concat.apply([], data.map(function (busLine, idx) {
                return {
                    coords: busLine,
                    lineStyle: {
                        normal: {
                            color: echarts.color.modifyHSL('#5A94DF', Math.round(hStep * idx))
                        }
                    }
                };

            }));
            busData = busLines;
            // console.log("busData数据："+busData)
        },
        error: function (data) {
            console.info("失败"+data);
        }
    })
    // var busLines = busData;
    var  busLines = busData;
    // console.log("busLines数据"+busLines);
    myChart.setOption(option = {
        bmap: {
            center: [118.727201,32.021921],
            zoom: 13,
            roam: true,
            mapStyle: {
                'styleJson': [
                    {
                        'featureType': 'water',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#d1d1d1'
                        }
                    }, {
                        'featureType': 'land',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#f3f3f3'
                        }
                    }, {
                        'featureType': 'railway',
                        'elementType': 'all',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'highway',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#fdfdfd'
                        }
                    }, {
                        'featureType': 'highway',
                        'elementType': 'labels',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'arterial',
                        'elementType': 'geometry',
                        'stylers': {
                            'color': '#fefefe'
                        }
                    }, {
                        'featureType': 'arterial',
                        'elementType': 'geometry.fill',
                        'stylers': {
                            'color': '#fefefe'
                        }
                    }, {
                        'featureType': 'poi',
                        'elementType': 'all',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'green',
                        'elementType': 'all',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'subway',
                        'elementType': 'all',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'manmade',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#d1d1d1'
                        }
                    }, {
                        'featureType': 'local',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#d1d1d1'
                        }
                    }, {
                        'featureType': 'arterial',
                        'elementType': 'labels',
                        'stylers': {
                            'visibility': 'off'
                        }
                    }, {
                        'featureType': 'boundary',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#fefefe'
                        }
                    }, {
                        'featureType': 'building',
                        'elementType': 'all',
                        'stylers': {
                            'color': '#d1d1d1'
                        }
                    }, {
                        'featureType': 'label',
                        'elementType': 'labels.text.fill',
                        'stylers': {
                            'color': '#999999'
                        }
                    }
                ]
            }
        },
        series: [{
            type: 'lines',
            coordinateSystem: 'bmap',
            polyline: true,
            data: busLines,
            silent: true,
            lineStyle: {
                normal: {
                    // color: '#c23531',
                    // color: 'rgb(200, 35, 45)',
                    opacity: 0.3,

                    width: 2
                }
            },
            progressiveThreshold: 500,
            progressive: 200
        }, {
            type: 'lines',
            coordinateSystem: 'bmap',
            polyline: true,
            data: busLines,
            lineStyle: {
                normal: {
                    width: 0
                }
            },
            effect: {
                constantSpeed: 50,
                show: true,
                trailLength: 0.5,
                symbolSize: 2
            },
            zlevel: 1
        },
            {
                type: 'custom',
                coordinateSystem: 'bmap',
                renderItem: renderItem,
                itemStyle: {
                    opacity: 0.8
                },
                animation: false,
                silent: true,
                data: [0],
                z: -10
            }
        ]
    });
});

function renderItem(params, api) {
    var coords = [
        [118.759926,32.068626],
        [118.802875,32.068626],
        [118.802875,32.046348],
        [118.759926,32.046348],
        [118.759926,32.068626]
    ];
    var points = [];
    for (var i = 0; i < coords.length; i++) {
        points.push(api.coord(coords[i]));
    }
    var color = api.visual('color');

    return {
        type: 'polygon',
        shape: {
            points: echarts.graphic.clipPointsByRect(points, {
                x: params.coordSys.x,
                y: params.coordSys.y,
                width: params.coordSys.width,
                height: params.coordSys.height
            })
        },
        style: api.style({
            fill: color,
            stroke: echarts.color.lift(color)
        })
    };
}